let app = getApp();
import Notify from '@vant/weapp/notify/notify';
let that;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusOptions: [{
        text: "全部",
        value: ""
      },
      {
        text: "未使用",
        value: "未使用"
      },
      {
        text: "已被领取",
        value: "已被领取"
      },
      {
        text: "已使用",
        value: "已使用"
      }
    ],
    status: "",
    typeOptions: [{
        text: "所有类型",
        value: ""
      }, {
        text: "月度卡密",
        value: "month"
      },
      {
        text: "季度卡密",
        value: "season"
      },
      {
        text: "年度卡密",
        value: "year"
      },
      {
        text: "永久卡密",
        value: "forever"
      }
    ],
    type: "",
    cards: [],
    typeValue: 'month',
    typeName: '月度用户',
    count: 10,
    showDialogFlag: false,
    pageno: 1,
    pagesize: 10,
    total: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    that = this;
    this.loadCardKey('');
    this.setData({
      beforeClose: (action) => this.onBeforeClose(action)
    })
  },
  onBeforeClose(action) {
    return new Promise((resolve) => {
      if (action == 'confirm') {
        let count = that.data.count;
        if (count > 50) {
          Notify({
            type: 'danger',
            message: '生成数量大于50',
            selector: '#notify_selector'
          })
          resolve(false)
          return
        }
        let typeValue = that.data.typeValue
        let option = {
          type: typeValue,
          count: count
        }
        app.request("card/generate", "POST", option).then(res => {
          Notify({
            type: 'success',
            message: '生成成功',
            selector: '#notify_selector'
          })
          that.setData({
            pageno:1
          })
          that.loadCardKey('')
          resolve(true)
        }).catch(e => {
          Notify({
            type: 'danger',
            message: e.msg,
            selector: '#notify_selector'
          })
          resolve(false)
        })
      } else {
        that.setData({
          showDialogFlag: false
        })
        resolve(false)
      }
    })
  },
  loadCardKey(t) {
    let options = {
      pageNo: this.data.pageno,
      pageSize: this.data.pagesize,
      orderBy: {
        "status": "desc"
      }
    }
    let status = this.data.status;
    let type = this.data.type;
    if (status) {
      options['status'] = ['eq', status];
    }
    if (type) {
      options['key_type'] = ['eq', type];
    }
    app.request('card/select/by/page', 'POST', options).then(res => {
      let total = res.data.total;
      let list = res.data.list;
      let cards = this.data.cards;

      if (t) {
        for (let i = 0; i < list.length; i++) {
          let item = list[i];
          cards.push(item)
        }
      }else{
        cards = list;
      }

      that.setData({
        cards: cards,
        total: res.data.total
      })
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let cards = this.data.cards;
    let le = cards.length;
    let total = this.data.total;
    if (le < total) {
      let pageno = ++this.data.pageno;
      this.setData({
        pageno: pageno
      })
      this.loadCardKey("reach")
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  changeItem() {
    this.loadCardKey('')
  },
  changeTypeValue(e) {
    let v = e.target.dataset.v;
    let n = e._relatedInfo.anchorTargetText;
    this.setData({
      typeValue: v,
      typeName: n
    })
  },
  showDialog() {
    this.setData({
      showDialogFlag: true
    })
  },
  getCode(e) {
    let id = e.currentTarget.dataset.id;
    app.request("card/get/code", "POST", {
      "id": id
    }, "xform").then(res => {
      let data = res.data[0];
      data.status = "已被领取";
      app.request("card/update", "POST", data).then(res => {
        Notify({
          type: 'success',
          message: '领取成功',
          selector: '#notify_selector'
        })
        that.clipboard(data.codeKey);
        that.loadCardKey('')
      })
    })
  },
  clipboard(data) {
    wx.setClipboardData({
      data: data
    })
  },
  onRefresh() {
    this.setData({
      pageno:1
    })
    this.loadCardKey('')
  }
})
